package fairygui.editor.gui
{
   import fairygui.editor.ComDocument;
   import fairygui.editor.Consts;
   import fairygui.editor.FairyGUIEditor;
   import fairygui.editor.animation.AniDef;
   import fairygui.editor.animation.AniImporter;
   import fairygui.editor.gui.text.EBitmapFont;
   import fairygui.editor.handlers.PackageRefreshHandler;
   import fairygui.editor.loader.EasyLoader;
   import fairygui.editor.loader.LoaderExt;
   import fairygui.editor.plugin.IEditorUIPackage;
   import fairygui.editor.settings.AtlasSettings;
   import fairygui.editor.settings.ImageSetting;
   import fairygui.editor.settings.PublishSettings;
   import fairygui.editor.utils.Callback;
   import fairygui.editor.utils.GIFDecoder;
   import fairygui.editor.utils.GIFFrame;
   import fairygui.editor.utils.ImageTool;
   import fairygui.editor.utils.ResourceSize;
   import fairygui.editor.utils.UtilsFile;
   import fairygui.editor.utils.UtilsStr;
   import fairygui.utils.GTimers;
   import flash.display.Bitmap;
   import flash.display.BitmapData;
   import flash.display.PNGEncoderOptions;
   import flash.filesystem.File;
   import flash.media.Sound;
   import flash.net.URLRequest;
   import flash.system.System;
   import flash.utils.ByteArray;
   
   public class EUIPackage implements IEditorUIPackage
   {
      
      private static var helperList:Array = [];
       
      
      private var _project:EUIProject;
      
      private var _id:String;
      
      private var _basePath:String;
      
      private var _nextId:uint;
      
      private var _opened:Boolean;
      
      private var _opening:Boolean;
      
      private var _rootItem:EPackageItem;
      
      private var _itemList:Vector.<EPackageItem>;
      
      private var _items:Object;
      
      private var _publishSettings:PublishSettings;
      
      private var _jpegQuality:int;
      
      private var _compressPNG:Boolean;
      
      private var _refreshHandler:PackageRefreshHandler;
      
      public var needSave:Boolean;
      
      public var descModification:Number;
      
      public var descFileSize:Number;
      
      public var publishing:Boolean;
      
      public function EUIPackage(param1:EUIProject, param2:String)
      {
         var descFile:File = null;
         var str:String = null;
         var project:EUIProject = param1;
         var name:String = param2;
         super();
         this._project = project;
         this._basePath = project.assetsPath + File.separator + name;
         this._items = {};
         this._itemList = new Vector.<EPackageItem>();
         this._refreshHandler = new PackageRefreshHandler(this);
         this._rootItem = new EPackageItem(this,EPackageItem.FOLDER);
         this._rootItem.id = "/";
         this._rootItem.name = name;
         this._rootItem.fileName = "";
         this._rootItem.path = "";
         this._rootItem.children = new Vector.<EPackageItem>();
         this._items["/"] = this._rootItem;
         this._rootItem.init();
         try
         {
            descFile = new File(this._basePath + File.separator + "package.xml");
            str = UtilsFile.loadString(descFile,null,500);
            this._id = UtilsStr.getBetweenSymbol(str,"id=\"","\"");
            if(this._id === null)
            {
               this._rootItem.errorStatus = 1;
               this._id = UtilsStr.generateUID();
               return;
            }
            str = UtilsStr.getBetweenSymbol(str,"hasFavorites=\"","\"");
            this._rootItem.favorite = str == "true";
            return;
         }
         catch(err:Error)
         {
            _rootItem.errorStatus = 2;
            _id = UtilsStr.generateUID();
            return;
         }
      }
      
      public function get opened() : Boolean
      {
         return this._opened;
      }
      
      public function get project() : EUIProject
      {
         return this._project;
      }
      
      public function get id() : String
      {
         return this._id;
      }
      
      public function get name() : String
      {
         return this._rootItem.name;
      }
      
      public function get basePath() : String
      {
         return this._basePath;
      }
      
      public function get resources() : Vector.<EPackageItem>
      {
         return this._itemList;
      }
      
      public function get publishSettings() : PublishSettings
      {
         this.ensureOpen();
         return this._publishSettings;
      }
      
      public function get jpegQuality() : int
      {
         this.ensureOpen();
         return this._jpegQuality;
      }
      
      public function set jpegQuality(param1:int) : void
      {
         this.ensureOpen();
         this._jpegQuality = param1;
      }
      
      public function get compressPNG() : Boolean
      {
         this.ensureOpen();
         return this._compressPNG;
      }
      
      public function set compressPNG(param1:Boolean) : void
      {
         this.ensureOpen();
         this._compressPNG = param1;
      }
      
      public function get rootItem() : EPackageItem
      {
         return this._rootItem;
      }
      
      public function toString() : String
      {
         return this._rootItem.name;
      }
      
      public function open() : void
      {
         var xml:XML = null;
         var cxml:XML = null;
         var str:String = null;
         var arr:Array = null;
         var maxAtlasSize:int = 0;
         var dxml:XML = null;
         var pi:EPackageItem = null;
         var descFile:File = null;
         var ats:AtlasSettings = null;
         var folder:EPackageItem = null;
         var intId:int = 0;
         var ii:ImageSetting = null;
         var reopen:Boolean = this._opened;
         this._opened = true;
         this._opening = true;
         this._itemList.length = 0;
         var oldItems:Object = this._items;
         this._items = {"/":this._rootItem};
         this._rootItem.children.length = 0;
         XML.ignoreWhitespace = true;
         try
         {
            descFile = new File(this._basePath + "/package.xml");
            str = UtilsFile.loadString(descFile);
            xml = new XML(str);
            this.descModification = descFile.modificationDate.time;
            this.descFileSize = descFile.size;
         }
         catch(e:Error)
         {
            _rootItem.errorStatus = 1;
            _opening = false;
            return;
         }
         str = xml.@jpegQuality;
         this._jpegQuality = parseInt(str);
         if(this._jpegQuality == 0)
         {
            this._jpegQuality = 80;
         }
         str = xml.@compressPNG;
         this._compressPNG = str != "false";
         this._nextId = 0;
         cxml = xml.publish[0];
         if(cxml == null)
         {
            cxml = <publish/>;
         }
         this._publishSettings = new PublishSettings();
         this._publishSettings.fileName = cxml.@name;
         if(this._publishSettings.fileName == null)
         {
            this._publishSettings.fileName = "";
         }
         this._publishSettings.filePath = cxml.@path;
         str = cxml.@packageCount;
         if(str)
         {
            this._publishSettings.packageCount = parseInt(str);
         }
         else
         {
            this._publishSettings.packageCount = 0;
         }
         this._publishSettings.genCode = cxml.@genCode == "true";
         str = cxml.@maxAtlasSize;
         if(str)
         {
            maxAtlasSize = parseInt(str);
         }
         else
         {
            maxAtlasSize = 2048;
         }
         var npot:Boolean = cxml.@npot == "true";
         var square:Boolean = cxml.@square == "true";
         var rotation:Boolean = cxml.@rotation == "true";
         var extractAlpha:Boolean = cxml.@extractAlpha == "true";
         var multiPage:Boolean = cxml.@multiPage != "false";
         var atlasCol:XMLList = cxml.atlas;
         var i:int = 0;
         while(i < this._publishSettings.atlasList.length)
         {
            ats = this._publishSettings.atlasList[i];
            ats.name = "";
            ats.pot = !npot;
            ats.square = square;
            ats.rotation = rotation;
            ats.maxHeight = ats.maxWidth = maxAtlasSize;
            ats.extractAlpha = extractAlpha;
            ats.compression = false;
            ats.multiPage = multiPage;
            i++;
         }
         for each(dxml in atlasCol)
         {
            ats = this._publishSettings.atlasList[dxml.@index];
            ats.name = dxml.@name;
            ats.compression = dxml.@compression == "true";
         }
         str = cxml.@excluded;
         if(str)
         {
            this._publishSettings.excludedList = str.split(",");
         }
         else
         {
            this._publishSettings.excludedList.length = 0;
         }
         this.listAllFolders(this._rootItem,new File(this._basePath),oldItems);
         this._rootItem.favorite = false;
         var col:XMLList = xml.resources.elements();
         for each(cxml in col)
         {
            str = cxml.@id;
            if(reopen)
            {
               pi = oldItems[str];
               if(!pi)
               {
                  pi = new EPackageItem(this,cxml.name().localName);
               }
            }
            else
            {
               pi = this._items[str];
               if(pi)
               {
                  continue;
               }
               pi = new EPackageItem(this,cxml.name().localName);
            }
            pi.id = str;
            pi.fileName = cxml.@name;
            pi.name = UtilsStr.getFileName(pi.fileName);
            pi.path = cxml.@path;
            if(pi.path.length == 0)
            {
               pi.path = "/";
            }
            else
            {
               if(pi.path.charAt(0) != "/")
               {
                  pi.path = "/" + pi.path;
               }
               if(pi.path.charAt(pi.path.length - 1) != "/")
               {
                  pi.path = pi.path + "/";
               }
            }
            pi.exported = cxml.@exported == "true";
            pi.favorite = cxml.@favorite == "true";
            if(pi.favorite)
            {
               this._rootItem.favorite = true;
            }
            if(pi.type == EPackageItem.IMAGE)
            {
               ii = pi.imageSetting;
               str = cxml.@scale;
               if(str)
               {
                  ii.scaleOption = str;
               }
               str = cxml.@scale9grid;
               if(str)
               {
                  arr = str.split(",");
                  ii.scale9Grid.x = arr[0];
                  ii.scale9Grid.y = arr[1];
                  ii.scale9Grid.width = arr[2];
                  ii.scale9Grid.height = arr[3];
               }
               str = cxml.@gridTile;
               if(str)
               {
                  ii.gridTile = parseInt(str);
               }
               else
               {
                  ii.gridTile = 0;
               }
               str = cxml.@qualityOption;
               if(str)
               {
                  ii.qualityOption = str;
               }
               str = cxml.@quality;
               if(str)
               {
                  ii.quality = int(str);
               }
               else
               {
                  ii.quality = this._jpegQuality;
               }
               ii.smoothing = cxml.@smoothing != "false";
               str = cxml.@atlas;
               if(str)
               {
                  ii.atlas = str;
               }
            }
            else if(pi.type == EPackageItem.MOVIECLIP)
            {
               ii = pi.imageSetting;
               str = cxml.@atlas;
               if(str)
               {
                  ii.atlas = str;
               }
            }
            else if(pi.type == EPackageItem.FONT)
            {
               pi.fontTexture = cxml.@texture;
            }
            folder = this._items[pi.path];
            if(!folder)
            {
               folder = this.ensurePathExists(pi.path,false,false);
            }
            folder.children.push(pi);
            this._items[pi.id] = pi;
            this._itemList.push(pi);
            pi.init();
            str = pi.id.substr(4);
            intId = parseInt(str,36);
            if(intId >= this._nextId)
            {
               this._nextId = intId + 1;
            }
         }
         this._opening = false;
         GTimers.inst.add((300 + Math.random() * 30) * 1000,0,this.gc);
      }
      
      public function save(param1:Boolean = true) : void
      {
         var cxml:XML = null;
         var dxml:XML = null;
         var pi:EPackageItem = null;
         var ats:AtlasSettings = null;
         var saveNow:Boolean = param1;
         if(!saveNow)
         {
            this.needSave = true;
            return;
         }
         this.needSave = false;
         var xml:XML = <packageDescription/>;
         xml.@id = this._id;
         xml.@jpegQuality = this._jpegQuality;
         xml.@compressPNG = this._compressPNG;
         cxml = <resources/>;
         xml.appendChild(cxml);
         this._rootItem.favorite = false;
         var cnt:int = this._itemList.length;
         var i:int = 0;
         while(i < cnt)
         {
            pi = this._itemList[i];
            if(pi.favorite && pi.type != EPackageItem.FOLDER)
            {
               this._rootItem.favorite = true;
            }
            if(pi.type != EPackageItem.FOLDER)
            {
               cxml.appendChild(pi.serialize());
            }
            i++;
         }
         if(this._rootItem.favorite)
         {
            xml.@hasFavorites = this._rootItem.favorite;
         }
         cxml = <publish/>;
         xml.appendChild(cxml);
         cxml.@name = this._publishSettings.fileName;
         if(this._publishSettings.filePath)
         {
            cxml.@path = this._publishSettings.filePath;
         }
         if(this._publishSettings.packageCount)
         {
            cxml.@packageCount = this._publishSettings.packageCount;
         }
         if(this._publishSettings.genCode)
         {
            cxml.@genCode = this._publishSettings.genCode;
         }
         var settings:AtlasSettings = this._publishSettings.atlasList[0];
         if(settings.maxWidth != 2048)
         {
            cxml.@maxAtlasSize = settings.maxWidth;
         }
         if(!settings.pot)
         {
            cxml.@npot = !settings.pot;
         }
         if(settings.square)
         {
            cxml.@square = settings.square;
         }
         if(settings.rotation)
         {
            cxml.@rotation = settings.rotation;
         }
         if(settings.extractAlpha)
         {
            cxml.@extractAlpha = settings.extractAlpha;
         }
         if(!settings.multiPage)
         {
            cxml.@multiPage = settings.multiPage;
         }
         i = 0;
         while(i < this._publishSettings.atlasList.length)
         {
            ats = this._publishSettings.atlasList[i];
            dxml = <atlas/>;
            if(ats.name)
            {
               dxml.@name = ats.name;
            }
            if(ats.compression)
            {
               dxml.@compression = ats.compression;
            }
            if(dxml.attributes().length() > 0)
            {
               dxml.@index = i;
               cxml.appendChild(dxml);
            }
            i++;
         }
         if(this._publishSettings.excludedList.length > 0)
         {
            cxml.@excluded = this._publishSettings.excludedList.join(",");
         }
         var descFile:File = new File(this._basePath + "/package.xml");
         try
         {
            UtilsFile.saveXML(descFile,xml);
         }
         catch(err:Error)
         {
            _project.editorWindow.alertError(err);
         }
         System.disposeXML(xml);
         this.descModification = descFile.modificationDate.time;
         this.descFileSize = descFile.size;
      }
      
      public function reload() : void
      {
         var _loc3_:EPackageItem = null;
         var _loc1_:int = this._itemList.length;
         var _loc2_:int = 0;
         while(_loc2_ < _loc1_)
         {
            _loc3_ = this._itemList[_loc2_];
            _loc3_.invalidate();
            _loc2_++;
         }
      }
      
      public function close() : void
      {
         GTimers.inst.remove(this.gc);
      }
      
      public function ensureOpen() : void
      {
         if(!this._opened)
         {
            this.open();
         }
      }
      
      public function refresh() : void
      {
         this._refreshHandler.run();
      }
      
      private function gc() : void
      {
         var _loc2_:EPackageItem = null;
         var _loc3_:AniDef = null;
         if(this.publishing)
         {
            return;
         }
         var _loc1_:Number = new Date().time / 1000;
         for each(_loc2_ in this._itemList)
         {
            if(_loc2_.type == EPackageItem.MOVIECLIP && _loc2_.data)
            {
               _loc3_ = AniDef(_loc2_.data);
               if(_loc3_.releasedTime && _loc1_ - _loc3_.releasedTime > 10)
               {
                  _loc3_.reset();
                  _loc2_.data = null;
               }
            }
         }
      }
      
      private function listAllFolders(param1:EPackageItem, param2:File, param3:Object) : void
      {
         var _loc7_:File = null;
         var _loc8_:* = null;
         var _loc9_:EPackageItem = null;
         var _loc4_:Array = param2.getDirectoryListing();
         var _loc5_:int = _loc4_.length;
         var _loc6_:int = 0;
         while(_loc6_ < _loc5_)
         {
            _loc7_ = _loc4_[_loc6_];
            if(_loc7_.isDirectory)
            {
               _loc8_ = param1.id + _loc7_.name + "/";
               _loc9_ = param3[_loc8_];
               if(!_loc9_)
               {
                  _loc9_ = new EPackageItem(this,EPackageItem.FOLDER);
               }
               _loc9_.fileName = _loc9_.name = _loc7_.name;
               _loc9_.path = param1.id;
               _loc9_.id = _loc8_;
               _loc9_.children = new Vector.<EPackageItem>();
               this._items[_loc9_.id] = _loc9_;
               this._itemList.push(_loc9_);
               param1.children.push(_loc9_);
               _loc9_.init();
               this.listAllFolders(_loc9_,_loc7_,param3);
            }
            _loc6_++;
         }
      }
      
      public function getNextId() : String
      {
         return EUIProject.devCode + (this._nextId++).toString(36);
      }
      
      public function getSequenceName(param1:String) : String
      {
         var _loc6_:EPackageItem = null;
         var _loc7_:int = 0;
         var _loc2_:int = param1.length;
         var _loc3_:int = this._itemList.length;
         var _loc4_:int = -1;
         var _loc5_:int = 0;
         while(_loc5_ < _loc3_)
         {
            _loc6_ = this._itemList[_loc5_];
            if(UtilsStr.startsWith(_loc6_.name,param1))
            {
               _loc7_ = parseInt(_loc6_.name.substr(_loc2_));
               if(_loc7_ > _loc4_)
               {
                  _loc4_ = _loc7_;
               }
            }
            _loc5_++;
         }
         if(_loc4_ <= 0)
         {
            return param1 + "1";
         }
         return param1 + ++_loc4_;
      }
      
      public function getUniqueName(param1:EPackageItem, param2:String, param3:Boolean = false) : String
      {
         var _loc6_:String = null;
         var _loc8_:int = 0;
         var _loc9_:int = 0;
         var _loc10_:int = 0;
         var _loc11_:EPackageItem = null;
         var _loc12_:Boolean = false;
         var _loc4_:String = UtilsStr.getFileName(param2);
         var _loc5_:String = UtilsStr.getFileExt(param2,true);
         if(_loc5_)
         {
            _loc6_ = "." + _loc5_;
         }
         _loc5_ = _loc5_.toLowerCase();
         var _loc7_:int = 1;
         var _loc13_:int = _loc4_.length;
         if(param3)
         {
            _loc8_ = this._itemList.length;
            _loc9_ = 0;
            while(_loc9_ < _loc8_)
            {
               _loc11_ = this._itemList[_loc9_];
               if(UtilsStr.startsWith(_loc11_.name,_loc4_,true) && UtilsStr.getFileExt(_loc11_.fileName) == _loc5_)
               {
                  if(_loc11_.name.length == _loc13_)
                  {
                     _loc12_ = true;
                  }
                  else if(_loc11_.name.charAt(_loc13_) == "(")
                  {
                     _loc10_ = _loc11_.name.indexOf(")",_loc13_);
                     if(_loc10_ != -1)
                     {
                        _loc10_ = parseInt(_loc11_.fileName.substring(_loc13_ + 1,_loc10_));
                        if(_loc10_ >= _loc7_)
                        {
                           _loc7_ = _loc10_ + 1;
                        }
                     }
                  }
               }
               _loc9_++;
            }
         }
         else
         {
            _loc8_ = param1.children.length;
            _loc9_ = 0;
            while(_loc9_ < _loc8_)
            {
               _loc11_ = param1.children[_loc9_];
               if(UtilsStr.startsWith(_loc11_.name,_loc4_,true) && UtilsStr.getFileExt(_loc11_.fileName) == _loc5_)
               {
                  if(_loc11_.name.length == _loc13_)
                  {
                     _loc12_ = true;
                  }
                  else if(_loc11_.name.charAt(_loc13_) == "(")
                  {
                     _loc10_ = _loc11_.name.indexOf(")",_loc13_);
                     if(_loc10_ != -1)
                     {
                        _loc10_ = parseInt(_loc11_.fileName.substring(_loc13_ + 1,_loc10_));
                        if(_loc10_ >= _loc7_)
                        {
                           _loc7_ = _loc10_ + 1;
                        }
                     }
                  }
               }
               _loc9_++;
            }
         }
         if(_loc12_)
         {
            return _loc4_ + "(" + _loc7_ + ")" + _loc6_;
         }
         return param2;
      }
      
      public function getFolderContent(param1:EPackageItem, param2:Array = null, param3:Boolean = true, param4:Vector.<EPackageItem> = null, param5:Boolean = false) : Vector.<EPackageItem>
      {
         var _loc8_:EPackageItem = null;
         this.ensureOpen();
         if(param4 == null)
         {
            param4 = new Vector.<EPackageItem>();
         }
         var _loc6_:int = param1.children.length;
         var _loc7_:int = 0;
         while(_loc7_ < _loc6_)
         {
            _loc8_ = param1.children[_loc7_];
            if(!(param2 != null && param2.indexOf(_loc8_.type) == -1))
            {
               param4.push(_loc8_);
               if(param5 && _loc8_.type == EPackageItem.FOLDER)
               {
                  this.getFolderContent(_loc8_,param2,param3,param4,param5);
               }
            }
            _loc7_++;
         }
         if(param3 && !param5)
         {
            param4.sort(this.compareItem);
         }
         return param4;
      }
      
      public function refreshFavoriteFlags(param1:EPackageItem) : void
      {
         var _loc4_:EPackageItem = null;
         this.ensureOpen();
         param1.favorite = false;
         var _loc2_:int = param1.children.length;
         var _loc3_:int = 0;
         while(_loc3_ < _loc2_)
         {
            _loc4_ = param1.children[_loc3_];
            if(_loc4_.type == EPackageItem.FOLDER)
            {
               this.refreshFavoriteFlags(_loc4_);
            }
            if(_loc4_.favorite)
            {
               param1.favorite = true;
            }
            _loc3_++;
         }
      }
      
      public function getFavoriteItems(param1:EPackageItem, param2:Boolean = true, param3:Vector.<EPackageItem> = null) : Vector.<EPackageItem>
      {
         var _loc6_:EPackageItem = null;
         this.ensureOpen();
         if(param3 == null)
         {
            param3 = new Vector.<EPackageItem>();
         }
         var _loc4_:int = param1.children.length;
         var _loc5_:int = 0;
         while(_loc5_ < _loc4_)
         {
            _loc6_ = param1.children[_loc5_];
            if(_loc6_.favorite)
            {
               param3.push(_loc6_);
            }
            _loc5_++;
         }
         if(param2)
         {
            param3.sort(this.compareItem);
         }
         return param3;
      }
      
      private function compareItem(param1:EPackageItem, param2:EPackageItem) : int
      {
         if(param1.type == "folder" && param2.type != "folder")
         {
            return -1;
         }
         if(param1.type != "folder" && param2.type == "folder")
         {
            return 1;
         }
         return param1.sortKey.localeCompare(param2.sortKey);
      }
      
      public function getItem(param1:String) : EPackageItem
      {
         this.ensureOpen();
         return this._items[param1];
      }
      
      public function getItemByName(param1:String) : EPackageItem
      {
         var _loc2_:EPackageItem = null;
         var _loc3_:EPackageItem = null;
         this.ensureOpen();
         for each(_loc2_ in this._itemList)
         {
            if(_loc2_.name == param1)
            {
               if(_loc2_.exported)
               {
                  helperList.unshift(_loc2_);
               }
               else
               {
                  helperList.push(_loc2_);
               }
            }
         }
         if(helperList.length > 0)
         {
            _loc3_ = helperList[0];
            helperList.length = 0;
            return _loc3_;
         }
         return null;
      }
      
      public function getItemByFileName(param1:EPackageItem, param2:String) : EPackageItem
      {
         var _loc5_:EPackageItem = null;
         var _loc3_:int = param1.children.length;
         var _loc4_:int = 0;
         while(_loc4_ < _loc3_)
         {
            _loc5_ = param1.children[_loc4_];
            if(_loc5_.fileName == param2)
            {
               return _loc5_;
            }
            _loc4_++;
         }
         return null;
      }
      
      public function getItemName(param1:String) : String
      {
         this.ensureOpen();
         var _loc2_:EPackageItem = this._items[param1];
         if(!_loc2_)
         {
            return null;
         }
         return _loc2_.name;
      }
      
      public function getItemPath(param1:EPackageItem) : Vector.<EPackageItem>
      {
         var _loc2_:Vector.<EPackageItem> = new Vector.<EPackageItem>();
         _loc2_.push(param1);
         if(param1 != this._rootItem)
         {
            param1 = this._items[param1.path];
            if(param1)
            {
               _loc2_.push(param1);
            }
         }
         _loc2_.reverse();
         return _loc2_;
      }
      
      public function addItem(param1:EPackageItem, param2:Boolean = true) : void
      {
         this._itemList.push(param1);
         this._items[param1.id] = param1;
         var _loc3_:EPackageItem = this._items[param1.path];
         _loc3_.children.push(param1);
         if(param1.type == EPackageItem.FOLDER)
         {
            if(!param1.children)
            {
               param1.children = new Vector.<EPackageItem>();
            }
         }
         param1.init();
         if(this._project.editorWindow && !this._opening)
         {
            this._project.editorWindow.mainPanel.libPanel.pkgsPanel.notifyChanged(_loc3_.treeNode);
         }
         this.save(param2);
      }
      
      public function renameItem(param1:EPackageItem, param2:String, param3:Boolean = true) : void
      {
         var _loc9_:File = null;
         var _loc10_:ComDocument = null;
         param2 = UtilsStr.validateName(param2);
         var _loc4_:File = param1.file;
         if(param1 == this._rootItem)
         {
            if(_loc4_.name != param2)
            {
               this.renameRoot(param2);
            }
            return;
         }
         var _loc5_:String = UtilsStr.getFileExt(param1.fileName,true);
         var _loc6_:File = new File(this._basePath + param1.path + param2 + (!!_loc5_?"." + _loc5_:""));
         var _loc7_:Boolean = !FairyGUIEditor.os_mac && _loc6_.name.toLowerCase() == _loc4_.name.toLowerCase();
         if(_loc6_.exists && !_loc7_)
         {
            throw new Error("file already exits");
         }
         if(_loc7_)
         {
            _loc9_ = new File(_loc6_.nativePath + "_" + Math.random() * 1000);
            _loc4_.moveTo(_loc9_);
            _loc9_.moveTo(_loc6_);
         }
         else
         {
            _loc4_.moveTo(_loc6_);
         }
         param1.name = param2;
         param1.fileName = _loc6_.name;
         param1.sortKey = null;
         if(param1.type == EPackageItem.FOLDER)
         {
            delete this._items[param1.id];
            param1.id = param1.path + param1.name + "/";
            this._items[param1.id] = param1;
            this.changeChildrenPath(param1);
         }
         param1.relocate();
         var _loc8_:EPackageItem = this._items[param1.path];
         if(this._project.editorWindow)
         {
            this._project.editorWindow.mainPanel.libPanel.pkgsPanel.notifyChanged(_loc8_.treeNode);
            _loc10_ = this._project.editorWindow.mainPanel.editPanel.findComDocument(param1.owner,param1.id);
            if(_loc10_ != null)
            {
               _loc10_.updateDocTitle();
            }
         }
         this._project.editorWindow.mainPanel.libPanel.updateItem(param1);
         this.save(param3);
      }
      
      private function renameRoot(param1:String) : void
      {
         var _loc5_:EPackageItem = null;
         var _loc6_:File = null;
         var _loc2_:String = this._project.assetsPath + "/" + param1;
         var _loc3_:File = new File(_loc2_);
         var _loc4_:Boolean = !FairyGUIEditor.os_mac && _loc3_.name.toLowerCase() == this._rootItem.file.name.toLowerCase();
         if(_loc3_.exists && !_loc4_)
         {
            throw new Error("file already exits");
         }
         if(_loc4_)
         {
            _loc6_ = new File(_loc3_.nativePath + "_" + Math.random() * 1000);
            this._rootItem.file.moveTo(_loc6_);
            _loc6_.moveTo(_loc3_);
         }
         else
         {
            this._rootItem.file.moveTo(_loc3_);
         }
         this._rootItem.name = param1;
         this._basePath = _loc2_;
         this._rootItem.sortKey = null;
         this._rootItem.relocate();
         for each(_loc5_ in this._itemList)
         {
            _loc5_.relocate();
         }
         this._project.editorWindow.mainPanel.libPanel.updateItem(this._rootItem);
         if(this._project.editorWindow)
         {
            this._project.editorWindow.mainPanel.libPanel.pkgsPanel.notifyRootChanged();
            this._project.editorWindow.mainPanel.editPanel.refreshDocument();
         }
      }
      
      public function moveItem(param1:EPackageItem, param2:String, param3:Boolean = true) : Boolean
      {
         if(param1.path == param2)
         {
            return false;
         }
         var _loc4_:EPackageItem = this._items[param1.path];
         var _loc5_:EPackageItem = this._items[param2];
         if(_loc4_ == null || _loc5_ == null)
         {
            return false;
         }
         var _loc6_:String = this.getUniqueName(_loc5_,param1.fileName,false);
         var _loc7_:File = param1.file;
         var _loc8_:File = new File(_loc5_.file.nativePath + "/" + _loc6_);
         if(param1.type == EPackageItem.FOLDER)
         {
            if(UtilsStr.startsWith(_loc8_.nativePath,_loc7_.nativePath,true))
            {
               return false;
            }
         }
         if(_loc7_.exists)
         {
            _loc7_.moveTo(_loc8_);
         }
         var _loc9_:int = _loc4_.children.indexOf(param1);
         _loc4_.children.splice(_loc9_,1);
         _loc5_.children.push(param1);
         param1.path = param2;
         param1.name = UtilsStr.getFileName(_loc6_);
         param1.fileName = _loc6_;
         if(param1.type == EPackageItem.FOLDER)
         {
            delete this._items[param1.id];
            param1.id = param1.path + param1.name + "/";
            this._items[param1.id] = param1;
            this.changeChildrenPath(param1);
         }
         param1.relocate();
         if(param1.treeNode && param1.treeNode.parent)
         {
            param1.treeNode.parent.removeChild(param1.treeNode);
         }
         if(this._project.editorWindow)
         {
            this._project.editorWindow.mainPanel.libPanel.pkgsPanel.notifyChanged(_loc5_.treeNode);
         }
         this.save(param3);
         return true;
      }
      
      private function changeChildrenPath(param1:EPackageItem) : void
      {
         var _loc4_:EPackageItem = null;
         var _loc2_:int = param1.children.length;
         var _loc3_:int = 0;
         while(_loc3_ < _loc2_)
         {
            _loc4_ = param1.children[_loc3_];
            _loc4_.path = param1.id;
            if(_loc4_.type == EPackageItem.FOLDER)
            {
               delete this._items[_loc4_.id];
               _loc4_.id = _loc4_.path + _loc4_.name + "/";
               this._items[_loc4_.id] = _loc4_;
               this.changeChildrenPath(_loc4_);
            }
            _loc4_.relocate();
            _loc3_++;
         }
      }
      
      public function deleteItem(param1:EPackageItem, param2:Boolean = true) : int
      {
         var _loc4_:EPackageItem = null;
         var _loc5_:int = 0;
         var _loc3_:int = this._deleteItem(param1);
         if(_loc3_ > 0)
         {
            if(param1.treeNode && param1.treeNode.parent)
            {
               param1.treeNode.parent.removeChild(param1.treeNode);
            }
            _loc4_ = this._items[param1.path];
            if(_loc4_ != null)
            {
               _loc5_ = _loc4_.children.indexOf(param1);
               _loc4_.children.splice(_loc5_,1);
            }
            this.save(param2);
         }
         return _loc3_;
      }
      
      public function deleteItems(param1:Vector.<EPackageItem>, param2:Boolean = true) : int
      {
         var _loc3_:int = param1.length;
         var _loc4_:int = 0;
         var _loc5_:int = 0;
         while(_loc5_ < _loc3_)
         {
            _loc4_ = _loc4_ + this.deleteItem(param1[_loc5_],false);
            _loc5_++;
         }
         this.save(param2);
         return _loc4_;
      }
      
      private function _deleteItem(param1:EPackageItem) : int
      {
         var _loc5_:int = 0;
         var _loc6_:ComDocument = null;
         var _loc2_:int = this._itemList.indexOf(param1);
         if(_loc2_ == -1)
         {
            return 0;
         }
         this._itemList.splice(_loc2_,1);
         delete this._items[param1.id];
         var _loc3_:int = 1;
         if(param1.type == EPackageItem.FOLDER)
         {
            _loc5_ = param1.children.length;
            _loc2_ = 0;
            while(_loc2_ < _loc5_)
            {
               _loc3_ = _loc3_ + this._deleteItem(param1.children[_loc2_]);
               _loc2_++;
            }
         }
         var _loc4_:File = param1.file;
         if(_loc4_.exists)
         {
            UtilsFile.deleteFile(_loc4_,true);
         }
         param1.invalidate();
         if(param1.type == EPackageItem.COMPONENT && this._project.editorWindow)
         {
            _loc6_ = this._project.editorWindow.mainPanel.editPanel.findComDocument(param1.owner,param1.id);
            if(_loc6_ != null)
            {
               this._project.editorWindow.mainPanel.editPanel.closeDocument(_loc6_);
            }
         }
         return _loc3_;
      }
      
      public function duplicateItem(param1:EPackageItem, param2:String, param3:Boolean = true) : EPackageItem
      {
         param2 = UtilsStr.validateName(param2);
         var _loc4_:String = UtilsStr.getFileExt(param1.fileName);
         param2 = param2 + (!!_loc4_?"." + _loc4_:"");
         var _loc5_:EPackageItem = new EPackageItem(this,param1.type);
         _loc5_.id = this.getNextId();
         _loc5_.name = UtilsStr.getFileName(param2);
         _loc5_.fileName = param2;
         _loc5_.path = param1.path;
         if(param1.type == EPackageItem.IMAGE || param1.type == EPackageItem.MOVIECLIP)
         {
            _loc5_.imageSetting.copyFrom(param1.imageSetting);
         }
         _loc5_.fontTexture = param1.fontTexture;
         var _loc6_:File = param1.file;
         var _loc7_:File = _loc5_.file;
         if(_loc7_.exists)
         {
            throw new Error("file already exists");
         }
         if(_loc6_.isDirectory)
         {
            _loc7_.createDirectory();
         }
         else
         {
            UtilsFile.copyFile(_loc6_,_loc7_);
         }
         this.addItem(_loc5_,param3);
         return _loc5_;
      }
      
      public function createFolder2(param1:EPackageItem, param2:String, param3:Boolean = true) : EPackageItem
      {
         this.ensureOpen();
         if(param2)
         {
            param2 = UtilsStr.validateName(param2);
         }
         else
         {
            param2 = this.getSequenceName("Folder");
         }
         var _loc4_:File = new File(param1.file.nativePath + "/" + param2);
         if(_loc4_.exists)
         {
            throw new Error("folder already exists");
         }
         _loc4_.createDirectory();
         var _loc5_:EPackageItem = new EPackageItem(this,EPackageItem.FOLDER);
         _loc5_.id = param1.id + param2 + "/";
         _loc5_.name = param2;
         _loc5_.fileName = param2;
         _loc5_.path = param1.id;
         this.addItem(_loc5_,param3);
         return _loc5_;
      }
      
      public function ensurePathExists(param1:String, param2:Boolean, param3:Boolean = true) : EPackageItem
      {
         var _loc7_:String = null;
         var _loc8_:Boolean = false;
         var _loc10_:File = null;
         var _loc4_:EPackageItem = this._items[param1];
         if(_loc4_)
         {
            return _loc4_;
         }
         _loc4_ = this._rootItem;
         var _loc5_:int = param1.length;
         var _loc6_:int = 1;
         var _loc9_:int = 1;
         while(_loc9_ < _loc5_)
         {
            if(param1.charAt(_loc9_) == "/")
            {
               _loc7_ = param1.substring(0,_loc9_ + 1);
               _loc4_ = this._items[_loc7_];
               if(!_loc4_)
               {
                  _loc4_ = new EPackageItem(this,EPackageItem.FOLDER);
                  _loc4_.id = _loc7_;
                  _loc4_.path = _loc7_.substring(0,_loc6_);
                  _loc4_.name = _loc4_.fileName = _loc7_.substring(_loc6_,_loc9_);
                  if(param2)
                  {
                     _loc10_ = _loc4_.file;
                     if(!_loc10_.exists)
                     {
                        _loc10_.createDirectory();
                     }
                  }
                  this.addItem(_loc4_,false);
                  _loc8_ = true;
               }
               _loc6_ = _loc9_ + 1;
            }
            _loc9_++;
         }
         if(_loc8_)
         {
            this.save(param3);
         }
         return _loc4_;
      }
      
      public function importResource(param1:File, param2:String, param3:Boolean, param4:Callback, param5:Boolean) : void
      {
         var type:String = null;
         var pi:EPackageItem = null;
         var callback2:Callback = null;
         var ani:AniDef = null;
         var xml:XML = null;
         var sourceFile:File = param1;
         var toPath:String = param2;
         var crop:Boolean = param3;
         var callback:Callback = param4;
         var saveNow:Boolean = param5;
         this.ensureOpen();
         var ext:String = sourceFile.extension.toLowerCase();
         if(ext == "swf")
         {
            type = EPackageItem.SWF;
         }
         else if(ext == "jta" || ext == "gif" || ext == "plist" || ext == "eas")
         {
            type = EPackageItem.MOVIECLIP;
         }
         else if(ext == "wav" || ext == "mp3" || ext == "ogg")
         {
            type = EPackageItem.SOUND;
         }
         else if(ext == "png" || ext == "jpg" || ext == "jpeg")
         {
            type = EPackageItem.IMAGE;
         }
         else if(ext == "fnt")
         {
            type = EPackageItem.FONT;
         }
         else if(ext == "xml")
         {
            type = EPackageItem.COMPONENT;
         }
         else
         {
            type = EPackageItem.MISC;
         }
         var folderItem:EPackageItem = this.ensurePathExists(toPath,true,false);
         var fileName:String = sourceFile.name;
         if(type == EPackageItem.MOVIECLIP)
         {
            fileName = UtilsStr.replaceFileExt(fileName,"jta");
         }
         fileName = this.getUniqueName(folderItem,fileName,false);
         pi = new EPackageItem(this,type);
         pi.id = this.getNextId();
         pi.path = toPath;
         pi.fileName = fileName;
         pi.name = UtilsStr.getFileName(fileName);
         callback2 = new Callback();
         callback2.success = function():void
         {
            addItem(pi,saveNow);
            callback.result = pi;
            callback.callOnSuccess();
         };
         callback2.failed = function():void
         {
            callback.addMsgs(callback2.msgs);
            callback.callOnFail();
         };
         var info:Object = ResourceSize.getSize(sourceFile);
         if(info && info.type == "png" && crop)
         {
            ImageTool.cropImage(sourceFile,pi.file,callback2);
         }
         else if(type == EPackageItem.MOVIECLIP)
         {
            if(ext == "gif")
            {
               this.importGif(pi,sourceFile,callback2);
            }
            else if(ext == "plist" || ext == "eas")
            {
               this.importMovieClip(pi,sourceFile,callback2);
            }
            else
            {
               ani = new AniDef();
               ani.load(UtilsFile.loadBytes(sourceFile));
               if(ani.frameCount == 0)
               {
                  callback.addMsg(Consts.g.text116);
                  callback.callOnFailImmediately();
               }
               else
               {
                  UtilsFile.saveBytes(pi.file,ani.save());
                  callback2.callOnSuccessImmediately();
               }
            }
         }
         else if(type == EPackageItem.FONT)
         {
            this.importFont(pi,sourceFile,callback2,saveNow);
         }
         else if(type == EPackageItem.COMPONENT)
         {
            try
            {
               xml = UtilsFile.loadXML(sourceFile);
               if(xml.name() != "component")
               {
                  callback2.addMsg("Not a valid fairygui component!");
                  callback2.callOnFailImmediately();
               }
               else
               {
                  UtilsFile.copyFile(sourceFile,pi.file);
                  callback2.callOnSuccessImmediately();
               }
            }
            catch(err:Error)
            {
               callback2.addMsg(err.message);
               callback2.callOnFailImmediately();
            }
         }
         else
         {
            UtilsFile.copyFile(sourceFile,pi.file);
            callback2.callOnSuccessImmediately();
         }
      }
      
      public function updateResource(param1:EPackageItem, param2:File, param3:Boolean, param4:Callback, param5:Boolean) : void
      {
         var callback2:Callback = null;
         var ani:AniDef = null;
         var xml:XML = null;
         var pi:EPackageItem = param1;
         var sourceFile:File = param2;
         var crop:Boolean = param3;
         var callback:Callback = param4;
         var saveNow:Boolean = param5;
         var info:Object = ResourceSize.getSize(sourceFile);
         if(info && ((info.type == "png" || info.type == "jpg") && pi.type != EPackageItem.IMAGE || info.type == "swf" && pi.type != EPackageItem.SWF || (info.type == "plist" || info.type == "eas" || info.type == "jta" || info.type == "gif") && pi.type != EPackageItem.MOVIECLIP || info.type == "xml" && pi.type != EPackageItem.COMPONENT))
         {
            callback.addMsg("Source file type mismatched!");
            callback.callOnFail();
            return;
         }
         var folderItem:EPackageItem = this.ensurePathExists(pi.path,true,false);
         var oldExt:String = UtilsStr.getFileExt(pi.fileName);
         var newExt:String = sourceFile.extension;
         var fileName:String = pi.fileName;
         if(pi.type == EPackageItem.IMAGE && oldExt != newExt)
         {
            fileName = UtilsStr.replaceFileExt(fileName,newExt);
            fileName = this.getUniqueName(folderItem,fileName,false);
            UtilsFile.deleteFile(pi.file);
            pi.fileName = fileName;
            this.save(saveNow);
         }
         callback2 = new Callback();
         callback2.success = function():void
         {
            pi.invalidate();
            callback.result = pi;
            callback.callOnSuccess();
         };
         callback2.failed = function():void
         {
            callback.addMsgs(callback2.msgs);
            callback.callOnFail();
         };
         if(info && info.type == "png" && crop)
         {
            ImageTool.cropImage(sourceFile,pi.file,callback2);
         }
         else if(pi.type == EPackageItem.MOVIECLIP)
         {
            if(newExt == "gif")
            {
               this.importGif(pi,sourceFile,callback2);
            }
            else if(newExt == "plist" || newExt == "eas")
            {
               this.importMovieClip(pi,sourceFile,callback2);
            }
            else
            {
               ani = new AniDef();
               ani.load(UtilsFile.loadBytes(sourceFile));
               if(ani.frameCount == 0)
               {
                  callback.addMsg(Consts.g.text116);
                  callback.callOnFailImmediately();
               }
               else
               {
                  UtilsFile.saveBytes(pi.file,ani.save());
                  callback2.callOnSuccessImmediately();
               }
            }
         }
         else if(pi.type == EPackageItem.FONT)
         {
            this.importFont(pi,sourceFile,callback2,saveNow);
         }
         else if(pi.type == EPackageItem.COMPONENT)
         {
            try
            {
               xml = UtilsFile.loadXML(sourceFile);
               if(xml.name() != "component")
               {
                  callback2.addMsg("Not a valid fairygui component!");
                  callback2.callOnFailImmediately();
               }
               else
               {
                  UtilsFile.copyFile(sourceFile,pi.file);
                  callback2.callOnSuccessImmediately();
               }
            }
            catch(err:Error)
            {
               callback2.addMsg(err.message);
               callback2.callOnFailImmediately();
            }
         }
         else
         {
            UtilsFile.copyFile(sourceFile,pi.file);
            callback2.callOnSuccessImmediately();
         }
      }
      
      private function importFont(param1:EPackageItem, param2:File, param3:Callback, param4:Boolean) : void
      {
         var i:int = 0;
         var pngFile:String = null;
         var ttf:Boolean = false;
         var str:String = null;
         var arr:Array = null;
         var j:int = 0;
         var arr2:Array = null;
         var atlasItem:EPackageItem = null;
         var newAtlas:Boolean = false;
         var sourcePngFile:File = null;
         var callback2:Callback = null;
         var pi:EPackageItem = param1;
         var sourceFile:File = param2;
         var callback:Callback = param3;
         var saveNow:Boolean = param4;
         var content:String = UtilsFile.loadString(sourceFile);
         var lines:Array = content.split("\n");
         var lineCount:int = lines.length;
         var kv:Object = {};
         i = 0;
         while(i < lineCount)
         {
            str = lines[i];
            if(str)
            {
               str = UtilsStr.trim(str);
               arr = str.split(" ");
               j = 1;
               while(j < arr.length)
               {
                  arr2 = arr[j].split("=");
                  kv[arr2[0]] = arr2[1];
                  j++;
               }
               str = arr[0];
               if(str == "page")
               {
                  pngFile = kv.file.substr(1,kv.file.length - 2);
                  break;
               }
               if(str == "info")
               {
                  ttf = kv.face != null;
               }
               else if(str == "common")
               {
                  if(int(kv.pages) > 1)
                  {
                     callback.addMsg(Consts.g.text114);
                     callback.callOnFail();
                     return;
                  }
               }
            }
            i++;
         }
         if(ttf)
         {
            if(pi.fontTexture)
            {
               atlasItem = this._items[pi.fontTexture];
            }
            if(!atlasItem)
            {
               atlasItem = new EPackageItem(this,EPackageItem.IMAGE);
               atlasItem.id = this.getNextId();
               atlasItem.path = pi.path;
               atlasItem.fileName = this.getUniqueName(this._items[pi.path],pi.name + "_atlas.png");
               atlasItem.name = UtilsStr.getFileName(atlasItem.fileName);
               newAtlas = true;
            }
            else
            {
               atlasItem.invalidate();
            }
            pi.fontTexture = atlasItem.id;
            sourcePngFile = new File(sourceFile.parent.nativePath + "/" + pngFile);
            if(!sourcePngFile.exists)
            {
               sourcePngFile = new File(sourceFile.parent.nativePath + "/" + UtilsStr.replaceFileExt(sourceFile.name,"png"));
            }
            if(!sourcePngFile.exists)
            {
               callback.addMsg("File not found: " + sourcePngFile.nativePath);
               callback.callOnFail();
               return;
            }
            callback2 = new Callback();
            callback2.success = function():void
            {
               UtilsFile.copyFile(sourcePngFile,atlasItem.file);
               UtilsFile.copyFile(sourceFile,pi.file);
               if(newAtlas)
               {
                  addItem(atlasItem,saveNow);
               }
               callback.callOnSuccessImmediately();
            };
            callback2.failed = function():void
            {
               callback.addMsgs(callback2.msgs);
               callback.callOnFailImmediately();
            };
            ImageTool.cropImage(sourcePngFile,atlasItem.file,callback2);
         }
         else
         {
            pi.fontTexture = null;
            UtilsFile.copyFile(sourceFile,pi.file);
            callback.callOnSuccess();
         }
      }
      
      private function importGif(param1:EPackageItem, param2:File, param3:Callback) : void
      {
         var bmd:BitmapData = null;
         var tmpFolder:File = null;
         var frameDelays:Array = null;
         var ani:AniDef = null;
         var callback2:Callback = null;
         var gf:GIFFrame = null;
         var fn:int = 0;
         var delay:int = 0;
         var tmpFile:File = null;
         var ba:ByteArray = null;
         var pi:EPackageItem = param1;
         var sourceFile:File = param2;
         var callback:Callback = param3;
         var gd:GIFDecoder = new GIFDecoder(UtilsFile.loadBytes(sourceFile));
         var tmpFiles:Array = [];
         tmpFolder = File.createTempDirectory();
         frameDelays = [];
         var frameCount:int = gd.getFrameCount();
         if(gd.hasError() || frameCount == 0)
         {
            callback.addMsg("GIF format error!");
            callback.callOnFail();
            return;
         }
         var frame0:BitmapData = gd.getFrame(0).bitmapData;
         var i:int = 0;
         while(i < frameCount)
         {
            gf = gd.getFrame(i);
            bmd = gf.bitmapData;
            delay = gf.delay > 0?int(gf.delay):100;
            frameDelays[i] = int(delay / 1000 * 24);
            tmpFile = new File(tmpFolder.nativePath + File.separator + i + ".png");
            ba = bmd.encode(bmd.rect,new PNGEncoderOptions());
            UtilsFile.saveBytes(tmpFile,ba);
            tmpFiles[i] = tmpFile;
            i++;
         }
         ani = new AniDef();
         callback2 = new Callback();
         callback2.success = function():void
         {
            var _loc1_:int = ani.frameCount;
            var _loc2_:int = 0;
            while(_loc2_ < _loc1_)
            {
               ani.frameList[_loc2_].delay = int(frameDelays[_loc2_]);
               _loc2_++;
            }
            UtilsFile.saveBytes(pi.file,ani.save());
            if(UtilsStr.startsWith(sourceFile.parent.nativePath,_basePath))
            {
               UtilsFile.deleteFile(sourceFile);
            }
            try
            {
               tmpFolder.deleteDirectory(true);
            }
            catch(e:Error)
            {
            }
            callback.callOnSuccessImmediately();
         };
         callback2.failed = function():void
         {
            try
            {
               tmpFolder.deleteDirectory(true);
            }
            catch(e:Error)
            {
            }
            callback.addMsgs(callback2.msgs);
            callback.callOnFailImmediately();
         };
         AniImporter.importImages(tmpFiles,ani,!this._project.usingAtlas,callback2);
      }
      
      private function importMovieClip(param1:EPackageItem, param2:File, param3:Callback) : void
      {
         var ani:AniDef = null;
         var callback2:Callback = null;
         var pi:EPackageItem = param1;
         var sourceFile:File = param2;
         var callback:Callback = param3;
         ani = new AniDef();
         callback2 = new Callback();
         callback2.success = function():void
         {
            UtilsFile.saveBytes(pi.file,ani.save());
            callback.callOnSuccessImmediately();
         };
         callback2.failed = function():void
         {
            callback.addMsgs(callback2.msgs);
            callback.callOnFailImmediately();
         };
         AniImporter.importSpriteSheet(sourceFile,ani,!this._project.usingAtlas,callback2);
      }
      
      public function getComponentXML(param1:EPackageItem, param2:Boolean = false) : XML
      {
         var xml:XML = null;
         var doc:ComDocument = null;
         var file:File = null;
         var pi:EPackageItem = param1;
         var findDoc:Boolean = param2;
         if(pi.data && pi.dataVersion == pi.version)
         {
            return pi.data as XML;
         }
         if(findDoc)
         {
            doc = this._project.editorWindow.mainPanel.editPanel.findComDocument(this,pi.id);
            if(doc && doc.isModified)
            {
               xml = doc.serialize();
            }
         }
         if(!xml)
         {
            file = pi.file;
            if(!file.exists)
            {
               return null;
            }
            try
            {
               xml = new XML(UtilsFile.loadString(file));
            }
            catch(err:Error)
            {
               return null;
            }
         }
         if(pi.data)
         {
            System.disposeXML(pi.data as XML);
         }
         pi.data = xml;
         pi.dataVersion = pi.version;
         return xml;
      }
      
      public function getSound(param1:EPackageItem, param2:Function) : void
      {
         if(param1.data && param1.dataVersion == param1.version)
         {
            param2(param1);
            return;
         }
         if(!param1.file.exists)
         {
            param1.data = null;
            param2(param1);
            return;
         }
         if(param1.data)
         {
            try
            {
               Sound(param1.data).close();
            }
            catch(err:*)
            {
            }
         }
         var _loc3_:String = UtilsStr.getFileExt(param1.fileName);
         if(_loc3_ == "mp3")
         {
            param1.data = new Sound(new URLRequest(param1.file.url));
            param1.dataVersion = param1.version;
            param2(param1);
         }
         else
         {
            if(!param1.loadQueue)
            {
               param1.loadQueue = [];
            }
            param1.loadQueue.push(param2);
            this._project.editorWindow.cacheManager.loadSound(param1);
         }
      }
      
      public function getImage(param1:EPackageItem, param2:Function, param3:Boolean = true) : void
      {
         if(!param1.loadQueue)
         {
            param1.loadQueue = [];
         }
         param1.loadQueue.push(param2);
         if(param1.data && param1.dataVersion == param1.version)
         {
            GTimers.inst.callLater(param1.invokeCallbacks);
            return;
         }
         if(!param1.file.exists)
         {
            param1.data = null;
            param1.imageInfo.file = null;
            GTimers.inst.callLater(param1.invokeCallbacks);
            return;
         }
         if(param1.loadQueue.length > 1)
         {
            if(param3)
            {
               param1.imageInfo.loadingToMemory = true;
            }
            return;
         }
         param1.imageInfo.loadingToMemory = param3;
         if(param1.imageInfo.targetQuality == 100 || !this._project.editorWindow)
         {
            if(param3)
            {
               EasyLoader.load(param1.file.url,{
                  "pi":param1,
                  "type":"image"
               },this.__imageLoaded);
            }
            else
            {
               param1.imageInfo.file = param1.file;
               GTimers.inst.callLater(param1.invokeCallbacks);
            }
         }
         else
         {
            this._project.editorWindow.cacheManager.loadImage(param1);
         }
      }
      
      private function __imageLoaded(param1:LoaderExt) : void
      {
         var _loc2_:Object = param1.props;
         var _loc3_:EPackageItem = _loc2_.pi;
         var _loc4_:Object = param1.content;
         if(_loc4_ is Bitmap)
         {
            _loc3_.data = _loc4_.bitmapData;
            _loc3_.imageInfo.file = _loc3_.file;
         }
         else
         {
            _loc3_.data = null;
            _loc3_.imageInfo.file = null;
         }
         _loc3_.dataVersion = _loc3_.version;
         _loc3_.invokeCallbacks();
      }
      
      public function getMovieClip(param1:EPackageItem) : AniDef
      {
         var ani:AniDef = null;
         var pi:EPackageItem = param1;
         if(pi.data && pi.dataVersion == pi.version)
         {
            return AniDef(pi.data);
         }
         if(pi.data)
         {
            AniDef(pi.data).reset();
            pi.data = null;
         }
         var ba:ByteArray = UtilsFile.loadBytes(pi.file);
         if(ba == null)
         {
            return null;
         }
         try
         {
            ani = new AniDef();
            ani.load(ba);
            pi.data = ani;
         }
         catch(err:Error)
         {
         }
         pi.dataVersion = pi.version;
         return AniDef(pi.data);
      }
      
      public function getBitmapFont(param1:EPackageItem) : EBitmapFont
      {
         if(param1.data && param1.dataVersion == param1.version)
         {
            return EBitmapFont(param1.data);
         }
         if(param1.data)
         {
            EBitmapFont(param1.data).dispose();
            param1.data = null;
         }
         var _loc2_:EBitmapFont = new EBitmapFont();
         _loc2_.packageItem = param1;
         param1.data = _loc2_;
         param1.dataVersion = param1.version;
         _loc2_.load();
         return EBitmapFont(param1.data);
      }
      
      public function getImageQuality(param1:EPackageItem) : int
      {
         var _loc2_:ImageSetting = param1.imageSetting;
         switch(_loc2_.qualityOption)
         {
            case ImageSetting.QUALITY_DEFAULT:
               if(this._project.usingAtlas)
               {
                  return 100;
               }
               if(param1.imageInfo.format == "png")
               {
                  return !!this._compressPNG?8:100;
               }
               return this._jpegQuality;
            case ImageSetting.QUALITY_SOURCE:
               return 100;
            case ImageSetting.QUALITY_CUSTOM:
               if(param1.imageInfo.format == "png")
               {
                  return this._jpegQuality != 100?8:100;
               }
               return _loc2_.quality;
            default:
               return 100;
         }
      }
      
      public function createNewComponent(param1:String, param2:int, param3:int, param4:String, param5:String = null, param6:XML = null, param7:Boolean = false, param8:Boolean = false) : EPackageItem
      {
         var _loc12_:XML = null;
         var _loc13_:File = null;
         var _loc14_:XML = null;
         this.ensureOpen();
         param1 = UtilsStr.validateName(param1);
         var _loc9_:EPackageItem = this._items[param4];
         var _loc10_:* = param1 + ".xml";
         if(param8)
         {
            _loc10_ = this.getUniqueName(_loc9_,_loc10_,true);
         }
         else
         {
            _loc13_ = new File(this._basePath + param4 + _loc10_);
            if(_loc13_.exists)
            {
               throw new Error(Consts.g.text113);
            }
         }
         var _loc11_:EPackageItem = new EPackageItem(this,EPackageItem.COMPONENT);
         _loc11_.id = this.getNextId();
         _loc11_.path = param4;
         _loc11_.name = UtilsStr.getFileName(_loc10_);
         _loc11_.fileName = _loc10_;
         _loc11_.exported = param7;
         if(param6 != null)
         {
            _loc12_ = param6;
         }
         else
         {
            _loc12_ = <component/>;
         }
         _loc12_.@size = param2 + "," + param3;
         if(param5)
         {
            _loc12_.@extention = param5;
            _loc14_ = new XML("<" + param5 + "/>");
            _loc12_.appendChild(_loc14_);
         }
         UtilsFile.saveXML(_loc11_.file,_loc12_);
         this.addItem(_loc11_);
         return _loc11_;
      }
      
      public function createNewFont(param1:String, param2:String) : EPackageItem
      {
         this.ensureOpen();
         param1 = UtilsStr.validateName(param1);
         var _loc3_:* = param1 + ".fnt";
         var _loc4_:File = new File(this._basePath + param2 + _loc3_);
         if(_loc4_.exists)
         {
            throw new Error(Consts.g.text113);
         }
         var _loc5_:EPackageItem = new EPackageItem(this,EPackageItem.FONT);
         _loc5_.id = this.getNextId();
         _loc5_.path = param2;
         _loc5_.name = UtilsStr.getFileName(_loc3_);
         _loc5_.fileName = _loc3_;
         _loc5_.exported = true;
         UtilsFile.saveString(_loc5_.file,"info creator=UIBuilder\n");
         this.addItem(_loc5_);
         return _loc5_;
      }
      
      public function createNewMovieClip(param1:String, param2:String, param3:Boolean = false) : EPackageItem
      {
         this.ensureOpen();
         param1 = UtilsStr.validateName(param1);
         var _loc4_:* = param1 + ".jta";
         var _loc5_:File = new File(this._basePath + param2 + _loc4_);
         if(_loc5_.exists)
         {
            throw new Error(Consts.g.text113);
         }
         var _loc6_:EPackageItem = new EPackageItem(this,EPackageItem.MOVIECLIP);
         _loc6_.id = this.getNextId();
         _loc6_.path = param2;
         _loc6_.name = UtilsStr.getFileName(_loc4_);
         _loc6_.fileName = _loc4_;
         var _loc7_:AniDef = new AniDef();
         UtilsFile.saveBytes(_loc6_.file,_loc7_.save());
         this.addItem(_loc6_);
         return _loc6_;
      }
      
      public function getResourceId(param1:String) : String
      {
         var _loc4_:EPackageItem = null;
         var _loc2_:int = this._itemList.length;
         var _loc3_:int = 0;
         while(_loc3_ < _loc2_)
         {
            _loc4_ = this._itemList[_loc3_];
            if(_loc4_.type != EPackageItem.FOLDER && _loc4_.name == param1)
            {
               return _loc4_.id;
            }
            _loc3_++;
         }
         return null;
      }
      
      public function setExported(param1:Array, param2:Boolean) : void
      {
         var _loc3_:String = null;
         var _loc4_:EPackageItem = null;
         for each(_loc3_ in param1)
         {
            _loc4_ = this._items[_loc3_];
            if(_loc4_)
            {
               _loc4_.exported = param2;
            }
         }
         this.save(true);
      }
      
      public function createFolder(param1:String, param2:String) : void
      {
         var _loc3_:EPackageItem = this._items[param1];
         if(_loc3_)
         {
            this.createFolder2(_loc3_,param2);
         }
      }
      
      public function renameResources(param1:Array, param2:Array) : void
      {
         var cnt:int = 0;
         var i:int = 0;
         var ids:Array = param1;
         var names:Array = param2;
         try
         {
            cnt = ids.length;
            i = 0;
            while(i < cnt)
            {
               this.renameItem(ids[i],names[i],false);
               i++;
            }
            return;
         }
         finally
         {
            this.save(true);
         }
      }
      
      public function importResources(param1:String, param2:Array, param3:Array, param4:Function) : void
      {
         var callback:Callback = null;
         var folderPath:String = param1;
         var files:Array = param2;
         var names:Array = param3;
         var onComplete:Function = param4;
         callback = new Callback();
         callback.success = function():void
         {
            if(onComplete != null)
            {
               if(onComplete.length > 0)
               {
                  onComplete(callback.result);
               }
               else
               {
                  onComplete();
               }
            }
         };
         this.project.editorWindow.mainPanel.importResources(files,this,[folderPath],callback);
      }
      
      public function createMovieClip(param1:String, param2:String, param3:Array, param4:Object, param5:Function, param6:Function) : void
      {
         var pi:EPackageItem = null;
         var ani:AniDef = null;
         var importCallback:Callback = null;
         var resName:String = param1;
         var folderPath:String = param2;
         var files:Array = param3;
         var options:Object = param4;
         var onSuccess:Function = param5;
         var onFail:Function = param6;
         pi = this.createNewMovieClip(resName,folderPath,true);
         ani = this.getMovieClip(pi);
         importCallback = new Callback();
         importCallback.success = function():void
         {
            if(options == null)
            {
               options = {};
            }
            if(options.speed)
            {
               ani.speed = options.speed;
            }
            ani.repeatDelay = options.repeatDelay;
            if(options.swing)
            {
               ani.swing = true;
            }
            UtilsFile.saveBytes(pi.file,ani.save());
            project.editorWindow.closeWaiting();
            if(onSuccess != null)
            {
               if(onSuccess.length > 0)
               {
                  onSuccess(pi.id);
               }
               else
               {
                  onSuccess();
               }
            }
         };
         importCallback.failed = function():void
         {
            project.editorWindow.closeWaiting();
            if(onFail != null)
            {
               if(onFail.length > 0)
               {
                  onFail(importCallback.msgs.join("\n"));
               }
               else
               {
                  onFail();
               }
            }
         };
         this.project.editorWindow.showWaiting(Consts.g.text86 + "...");
         AniImporter.importImages(files,ani,!this._project.usingAtlas,importCallback);
      }
      
      public function createComponent(param1:String, param2:int, param3:int, param4:String, param5:XML) : String
      {
         var _loc6_:EPackageItem = this.createNewComponent(param1,param2,param3,param4,null,param5,false);
         return _loc6_.id;
      }
   }
}
